package com.qersoft.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qersoft.dao.UserInfo;
import com.qersoft.dao.WQbank;
import com.qersoft.entity.DicUserEntity;
import com.qersoft.entity.UserSelect;
import com.qersoft.entity.WQBankEntity;
import com.qersoft.util.HashUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

@Service("UserInfoService")
public class UserInfoImpl extends ServiceImpl<UserInfo, DicUserEntity>
        implements UserInfoService {


    @Autowired
    private UserInfo userInfo;
    @Autowired
    private WQbank wQbank;

    public UserInfoImpl(UserInfo userInfo, WQbank wQbank) {
        this.userInfo = userInfo;
        this.wQbank = wQbank;
    }


    @Override
    public Page<DicUserEntity> querypage(UserSelect userSelect) {

        //分页对象
        Page<DicUserEntity> page=new Page<>(userSelect.getPage(),userSelect.getPageSize());
        //条件构造器
        QueryWrapper<DicUserEntity> queryWrapper=new QueryWrapper<>();

        if(userSelect.getUsername()!=null&& !userSelect.getUsername().isEmpty()){
            queryWrapper.like("username",userSelect.getUsername());
        }
        if(userSelect.getId()!=null)
        {
            queryWrapper.eq("id", userSelect.getId());
        }
        if(userSelect.getReal_name()!=null&& !userSelect.getReal_name().isEmpty()){
            queryWrapper.like("real_name",userSelect.getReal_name());
        }
        if(userSelect.getStatus()!=null){
            queryWrapper.eq("status",userSelect.getStatus());
        }
        if(userSelect.getPhone()!=null&& !userSelect.getPhone().isEmpty()){
            queryWrapper.like("phone",userSelect.getPhone());
        }
        if(userSelect.getUpperscore()!=null){
            queryWrapper.ge("score",userSelect.getUpperscore());
        }
        if(userSelect.getLowerscore()!=null){
            queryWrapper.le("score",userSelect.getLowerscore());
        }
        if(userSelect.getGender()!=null){
            queryWrapper.eq("gender",userSelect.getGender());
        }

        return userInfo.selectPage(page,queryWrapper);

    }

    @Override
    public DicUserEntity add(DicUserEntity dicUserEntity) {
        String password = Encryptor(dicUserEntity.getPassword());
        dicUserEntity.setPassword(password);
        userInfo.insert(dicUserEntity);
        WQBankEntity wqBankEntity = new WQBankEntity();
        wqBankEntity.setUserId(dicUserEntity.getId());
        wQbank.insert(wqBankEntity);
        return dicUserEntity;
    }

    @Override
    public DicUserEntity find(Long userId) {
        QueryWrapper<DicUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        DicUserEntity dicUserEntity = userInfo.selectOne(queryWrapper);
        return dicUserEntity;
    }

    @Override
    public DicUserEntity del(Long userId) {
        QueryWrapper<DicUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        DicUserEntity dicUserEntity = userInfo.selectOne(queryWrapper);
        userInfo.delete(queryWrapper);
        QueryWrapper<WQBankEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("userId", userId);
        wQbank.delete(queryWrapper1);
        return dicUserEntity;
    }

    @Override
    public DicUserEntity change(DicUserEntity dicUserEntity) {

        if(dicUserEntity.getId()==null)
        {
            return null;
        }
        userInfo.updateById(dicUserEntity);
        return dicUserEntity;
    }

    @Override
    public String login(String username,String password) {
        DicUserEntity dicUserEntity=userInfo.selectOne(new QueryWrapper<DicUserEntity>().eq("username", username));
        if(dicUserEntity==null)
            return "error:用户名或密码输入错误";
        else {
            String password_new=Encryptor(password);
            if(!password_new.equals(dicUserEntity.getPassword())){
                return "error:用户名或密码输入错误";
            }
        }

        return "success:成功登录，欢迎使用考试平台";
    }

    @Override
    public String cpw(Long userId, String password, String passwordNew) {
        DicUserEntity dicUserEntity=userInfo.selectOne(new QueryWrapper<DicUserEntity>().eq("id", userId));
        if(dicUserEntity==null)
        {
            return "error";
        }
        else {
            try {
                if(!HashUtils.hash(password).equals(dicUserEntity.getPassword()))
                {
                    return "error";
                }
                else{
                    dicUserEntity.setPassword(HashUtils.hash(passwordNew));
                    userInfo.updateById(dicUserEntity);
                    return "success";
                }
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private String Encryptor(String password){
        try {
            return HashUtils.hash(password);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}
